<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class MemberController extends AdminController {
    public function index(){
       $list = M('Member')->select();
       $this->assign('list',$list);
       $this->display();
    }
    public function insert(){
    	$name     = I('name');
    	$idcard   = I('idcard');
    	$password = I('password');
    	$nickname = I('nickname');
    	$phone    = I('phone');
    	if(empty($name)){
    		$this -> error('姓名不能为空');
    	}
    	if(empty($idcard)){
    		$this -> error('身份证号码不能为空');
    	}
    	if(empty($password)){
    		$this -> error('密码不能为空');
    	}
    	if(empty($nickname)){
    		$this -> error('昵称不能为空');
    	}
    	if(empty($phone)){
           $this -> error('电话不能为空');
    	}
    	if(empty($_FILES['pic']['name'])) {
			$this -> error('图片不能为空');
		}
	    $pic = $this ->upload($_FILES['pic']);
	    $data['pic'] = $pic;
	    $data['idcard'] = $idcard;
	    $data['name']   = $name;
	    $data['password'] = md5($password);
	    $data['nickname'] = $nickname;
	    $data['phone']    = $phone;
	    if(!M('Member')->add($data)){
	    	$this -> error('系统繁忙');
	    }
	    $this -> success('添加成功');

    }
    public function edit(){
        $member_id = I('member_id');
        $where['member_id'] = $member_id;
        $row = M('Member')->where($where)->find();
        $this -> assign('row',$row);
        $this -> display();
    }
    public function update(){
        $nickname = I('nickname');
        $phone    = I('phone');
        $pic      = I('pic');
        $member_id = I('member_id');
        if(empty($nickname)){
            $this -> error('昵称不能为空');
        }
        if(empty($phone)){
           $this -> error('电话不能为空');
        }
        if(!empty($_FILES['pic']['name'])) {
            $pic = $this ->upload($_FILES['pic']);
        }
        
        $data['pic'] = $pic;
        $data['nickname'] = $nickname;
        $data['phone']    = $phone;
        if(!M('Member')->where(array('member_id'=>$member_id))->save($data)){
            $this -> error('系统繁忙');
        }
        $this -> success('修改成功','index');
    }
    public function del(){
        $member_id = I('member_id');
        if(!M('Member')->where(array('member_id'=>$member_id))->delete()){
            $this -> error('系统繁忙');
        }
        $this -> success('删除成功','index');
    }
}